﻿using System;
using System.Collections.Generic;
using NakedObjects;
using NakedObjects.Value;
using System.Linq;

namespace HRMDomain
{
    [Bounded]
    public class Location:Base
    {

        [Title]
        public virtual string Name { get; set; }

        #region Meetings (collection)
        private ICollection<Meeting> _Meetings = new List<Meeting>();

        public virtual ICollection<Meeting> Meetings
        {
            get
            {
                return _Meetings;
            }
            set
            {
                _Meetings = value;
            }
        }
        #endregion
        #region Notes (collection)
        private ICollection<Note> _Notes = new List<Note>();

        public virtual ICollection<Note> Notes
        {
            get
            {
                return _Notes;
            }
            set
            {
                _Notes = value;
            }
        }
        #endregion
        #region FeedbackList (collection)
        private ICollection<Feedback> _FeedbackList = new List<Feedback>();

        public virtual ICollection<Feedback> FeedbackList
        {
            get
            {
                return _FeedbackList;
            }
            set
            {
                _FeedbackList = value;
            }
        }
        #endregion
        #region Documents (collection)
        private ICollection<Document> _Documents = new List<Document>();

        public virtual ICollection<Document> Documents
        {
            get
            {
                return _Documents;
            }
            set
            {
                _Documents = value;
            }
        }
        #endregion

        #region AddDocument (Action)

        [MemberOrder(Sequence = "1")]
        public void AddDocument(string Name, FileAttachment file)
        {
            Document newdoc = Container.NewTransientInstance<Document>();
            newdoc.Name = Name;
            newdoc.Data = file.GetResourceAsByteArray();
            newdoc.MIMEType = file.MimeType;

            Container.Persist<Document>(ref newdoc);

            _Documents.Add(newdoc);
            Container.InformUser("Document Attachment Added.");
        }

        // Use 'hide', 'dis', 'val', 'actdef', 'actcho' shortcuts to add supporting methods here.
        #endregion

        #region ListEmployees (Action)

        [MemberOrder(Sequence = "1")]
        public IQueryable<Employee> ListEmployees()
        {
            return Container.Instances<Employee>().Where(w => w.CurrentLocation.Id == this.Id).AsQueryable();
            //return StationHistory.Where(w => w.EndDate == null).Select(s => s.Station).FirstOrDefault();
        }

        // Use 'hide', 'dis', 'val', 'actdef', 'actcho' shortcuts to add supporting methods here.
        #endregion

        #region EmployeeHistory (Action)

        [MemberOrder(Sequence = "1")]
        public IQueryable<EmployeeLocation> EmployeeHistory()
        {
            return Container.Instances<EmployeeLocation>().Where(w => w.Location.Id == this.Id);
        }

        // Use 'hide', 'dis', 'val', 'actdef', 'actcho' shortcuts to add supporting methods here.
        #endregion

        #region AddNote (Action)

        [MemberOrder(Sequence = "1")]
        public void AddNote([MultiLine] string Entry)
        {
            Note newNote = Container.NewTransientInstance<Note>();

            newNote.EnteredDate = DateTime.Now;
            newNote.Text = Entry;
            Container.Persist<Note>(ref newNote);

            if (!(_Notes.Contains(newNote)))
            {
                _Notes.Add(newNote);
            }

            Container.InformUser("Note Added.");
        }

        // Use 'hide', 'dis', 'val', 'actdef', 'actcho' shortcuts to add supporting methods here.
        #endregion

        #region AddMeeting (Action)

        [MemberOrder(Sequence = "2")]
        public void AddMeeting(DateTime DatePerformed)
        {
            Meeting newMeeting = Container.NewTransientInstance<Meeting>();

            newMeeting.DatePerformed = DateTime.Now;

            Container.Persist<Meeting>(ref newMeeting);

            if (!(_Meetings.Contains(newMeeting)))
            {
                _Meetings.Add(newMeeting);
            }

            Container.InformUser("New Meeting Added.");
        }

        // Use 'hide', 'dis', 'val', 'actdef', 'actcho' shortcuts to add supporting methods here.
        #endregion

        #region AddFeedback (Action)

        [MemberOrder(Sequence = "1")]
        public void AddFeedback(FeedbackCategory Category, DateTime Date, [MultiLine]string Details)
        {
            Feedback newFeedback = Container.NewTransientInstance<Feedback>();

            newFeedback.Type = Category;
            newFeedback.Date = Date;
            newFeedback.Details = Details;

            Container.Persist<Feedback>(ref newFeedback);

            this._FeedbackList.Add(newFeedback);
            Container.InformUser("Feedback Added.");
        }

        // Use 'hide', 'dis', 'val', 'actdef', 'actcho' shortcuts to add supporting methods here.
        #endregion
    }
}
